const { ccclass, property } = cc._decorator;

@ccclass
export default class DragBase extends cc.Component {
    public prePos: cc.Vec2 = null;
    setPrePos(pos: cc.Vec2) {
        this.prePos = pos;
    }

    public isInArea: boolean = false;
    setIsInArea(bool: boolean) {
        this.isInArea = bool;
    }

    public targetPos: cc.Vec2 = null;
    setTargetPos(pos: cc.Vec2) {
        this.targetPos = pos;
    }

    @property({ type: cc.Prefab, displayName: "说话预制体" })
    public speakLabelPref: cc.Prefab = null;

    public speakLabel: cc.Node = null;

    public canMove: boolean = true;
    setCanMove(bool: boolean) {
        this.canMove = bool;
    }

    onLoad() {
    }

    start() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.moveComponentStart.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.moveComponent.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_END, this.moveComponentEnd.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.moveComponentEnd.bind(this));
    }

    // update (dt) {}

    private isSpeak: boolean = false;
    speak(word: string) {
        if (this.isSpeak && this.speakLabel) {
            this.speakLabel.removeFromParent();
            this.speakLabel = null;
        }
        this.isSpeak = true;

        this.speakLabel = cc.instantiate(this.speakLabelPref);
        this.node.addChild(this.speakLabel);

        this.speakLabel.getComponent(cc.Label).string = word;
        cc.tween(this.speakLabel).to(0.2, { opacity: 255 }).delay(2).to(0.2, { opacity: 0 }).call(() => {
            this.isSpeak = false;
            this.speakLabel.removeFromParent();
            this.speakLabel = null;
        }).start();
    }

    goTargetPos() {
        cc.tween(this.node).to(0.1, { x: this.targetPos.x, y: this.targetPos.y }).start();
        this.node.off(cc.Node.EventType.TOUCH_START);
        this.node.off(cc.Node.EventType.TOUCH_MOVE);
        this.node.off(cc.Node.EventType.TOUCH_END);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL);
    }

    goPrePos() {
        cc.tween(this.node).to(0.1, { x: this.prePos.x, y: this.prePos.y }).start();
    }

    moveComponentStart(touchEvent: cc.Event.EventTouch): void {
        if (!this.canMove) return;
    }

    moveComponent(touchEvent: cc.Event.EventTouch): void {
        if (!this.canMove) return;
        let touchPos: cc.Vec2 = touchEvent.getLocation();
        let nextPos: cc.Vec2 = this.node.parent.convertToNodeSpaceAR(touchPos);
        this.node.setPosition(nextPos);
    }

    moveComponentEnd(touchEvent: cc.Event.EventTouch): void {
        if (!this.canMove) return;

        if (!this.isInArea && this.prePos != null) {
            cc.tween(this.node).to(0.2, { x: this.prePos.x, y: this.prePos.y }).start();
        } else if (this.isInArea) {
            this.goTargetPos();
        }
    }

    onCollisionEnter(other: cc.Collider, self: cc.Collider) {
    }

    onCollisionStay(other: cc.Collider, self: cc.Collider) {
    }

    onCollisionExit(other: cc.Collider, self: cc.Collider) {
    }
}
